﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace FmcWebsite.Core
{
    public class Category
    {
        private int id = -1;
        private int parent_id = -1;
        private string name = "";
        private string code = "";

        public Category()
        {
        }

        private Category(DataRow cat_row)
        {
            id = cat_row.Field<int>("category_id");
            parent_id = DBConnect.TryGetId(cat_row, "cat_category_id");
            name = cat_row.Field<string>("name");
            code = cat_row.Field<string>("code");
        }

        public int Id
        {
            get { return id; }
        }

        public Category Parent
        {
            get { return Category.ById(parent_id); }
        }

        public IEnumerable<Category> Children
        {
            get
            {
                return Category.AllCategories.Where(x => x.parent_id == id);
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Code
        {
            get { return code; }
        }

        public bool IsNull()
        {
            return id == -1;
        }

        public override string ToString()
        {
            return name;
        }

        static public Category Empty()
        {
            return new EmptyCategory();
        }        

        #region "database"

        private IEnumerable<DbParameter> GetParameters()
        {
            return new DbParameter[] 
                {
                    DBConnect.Database.CreateParameter("@catid", id),
                    DBConnect.Database.CreateParameter("@name", name),
                    DBConnect.Database.CreateParameter("@parentid", parent_id)

                };
        }

        public void Update()
        {

        }

        #endregion

        #region Static parts
        // categories do not meant to be much, we can load it all in a static memory

        static private List<Category> all_categories = null;

        static private IEnumerable<Category> RetrieveAll()
        {
            var tbl = DBConnect.Database.Select("SELECT * FROM category");
            foreach (DataRow r in tbl.Rows)
            {
                yield return new Category(r);
            }
        }

        static public IEnumerable<Category> AllCategories
        {
            get 
            {
                if (all_categories == null)
                {
                    all_categories = new List<Category>(RetrieveAll());
                }
                return all_categories;
            }
        }

        static public Category ById(int cat_id)
        {
            var search = AllCategories.Select(x => x).Where(x => x.id == cat_id);
            return search.Count() > 0 ? search.First() : null;
        }

        static public Category ByCode(string code)
        {
            var search = AllCategories.Where(x => x.code.Equals(code, StringComparison.OrdinalIgnoreCase)).Select(x => x);
            return search.Count() > 0 ? search.First() : null;
        }

        static private void GetUniverseAsList(Category parent, List<Category> universe)
        {
            universe.Add(parent);
            foreach (Category c in parent.Children)
                GetUniverseAsList(c, universe);
        }

        static public IEnumerable<Category> GetUniverse(Category parent)
        {
            List<Category> universe = new List<Category>();
            GetUniverseAsList(parent, universe);
            return universe;
        }

        static public void RefreshList()
        {
            all_categories = null;
            RetrieveAll();
        }

        #endregion

    }

    class EmptyCategory : Category
    {
    }
}
